import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/gt_selector_v3.dart';

class GTPickerShowSelectorV3 extends StatefulWidget {
  final List<GTSelectorModalV3> list;
  final ValueChanged<int>? onSingleClick;
  final int? selectIndex;
  final String? selectorTitle;
  final bool?needClear;
  const GTPickerShowSelectorV3({
    super.key,
    required this.list,
    this.onSingleClick,
    this.selectIndex,
    this.selectorTitle,
    this.needClear,
  });

  @override
  State<StatefulWidget> createState() => _PickerShowSelectorV3();
}

class _PickerShowSelectorV3 extends State<GTPickerShowSelectorV3> {
  int _index = -1;

  @override
  void initState() {
    super.initState();
    _index = widget.selectIndex ?? -1;
  }

  @override
  void didUpdateWidget(covariant GTPickerShowSelectorV3 oldWidget) {
    super.didUpdateWidget(oldWidget);
    print("widget.selectIndex==${widget.selectIndex}");
    if (widget.selectIndex != null && widget.selectIndex != _index) {
      setState(() {
        _index = widget.selectIndex!;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if((widget.selectorTitle ?? "").isNotEmpty)...[
          Text(
            widget.selectorTitle ?? "",
            style: gtTextStyleW500(fontSize: 14, color: GTColorsV3.text1.color),
          ),
          const SizedBox(
            height: 8,
          ),
        ],
        GTSelectorV3(
          list: widget.list,
          crossAxisCount: 4,
          showCheck: false,
          needClear: widget.needClear,
          selectIndexes: [_index],
          onSingleClick: (index, isChecked) {
            widget.onSingleClick?.call(index);
          },
        ),
      ],
    );
  }
}
